/**
 * Handles the class DBPartOrderTest.
 * 
 * Author: Group 3
 * Date: 07-04-2014 Version: 1.0
 */
package testlayer;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import modellayer.PartOrder;
import modellayer.Product;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dblayer.DBInvoice;
import dblayer.DBPartOrder;
import dblayer.DBConnection;
import dblayer.DBProduct;


public class DBPartOrderTest {
	
	private static DBPartOrder testPartOrderDB = new DBPartOrder();
	private static PartOrder testPartOrderInsert = new PartOrder();
	private static DBConnection testConnection = DBConnection.getInstance();
	private static DBProduct testProductDB = new DBProduct();

	
	@BeforeClass
	/**
	 * 
	 * @throws SQLException
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static void setUp() throws SQLException, UnsupportedEncodingException, FileNotFoundException
	{
		Product p = testProductDB.selectSingleProduct(1, true);
		
		testConnection.resetDatabase();
		
		testPartOrderInsert.setAmount(1);
		testPartOrderInsert.setPartOrderID(1);
		testPartOrderInsert.setProduct(p);
		testPartOrderInsert.setSalesOrderID(1);
	}
	
	@AfterClass
	public static void tearDown() throws SQLException, FileNotFoundException {
		
		testConnection.resetDatabase();
	}
	
	/**
	 * Tests if you can get all the part orders
	 */
	@Test
	public void testGetAllPartOrders() {
		assertNotNull(testPartOrderDB.getAllPartOrders(true));
	}

	/**
	 * Tests if you can select a single part order
	 * @throws SQLException
	 */
	@Test
	public void testSelectSinglePartOrder() throws SQLException {
		assertNotNull(testPartOrderDB.selectSinglePartOrder(1, true));
	}

	/**
	 * Tests if you can insert a part order to the database
	 * @throws SQLException
	 */
	@Test
	public void testInsertPartOrder() throws SQLException {
		assertNotSame(-1, testPartOrderDB.insertPartOrder(testPartOrderInsert));
	}

	/**
	 * Tests if you can update a part order in the database
	 * @throws SQLException
	 */
	@Test
	public void testUpdatePartOrder() throws SQLException {
		testPartOrderDB.selectSinglePartOrder(1, true).setAmount(2);
		assertNotSame(-1, testPartOrderDB.updatePartOrder(testPartOrderDB.selectSinglePartOrder(1, true)));
	}

	/**
	 * tests if you can delete a partirder in the database
	 * @throws SQLException
	 */
	@Test
	public void testDeletePartORder() throws SQLException {
		
		assertNotSame(-1, testPartOrderDB.deletePartOrder(testPartOrderDB.selectSinglePartOrder(1, true)));

}
}
